package com.rotten;

import imageLoader.DrawableManager;

import java.util.Iterator;
import java.util.Set;
import java.util.Vector;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class List_favorites extends Activity {
	
	DrawableManager draw = new DrawableManager();
	private int pag_actual = 1;
	Movie mov = new Movie();
	Favorites fav = new Favorites();
		
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
      //full screen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);   
        
        Bundle extras = getIntent().getExtras(); 
        if(extras !=null)
        {
        pag_actual = extras.getInt("pagina");
        } 
        
        setContentView(R.layout.list_favorites); 
        actualizaFavoritos();
	}
	
	public void actualizaFavoritos(){
	
        int total_favoritos = Main.favoritos.size();
       
        //Trata do layout
        LinearLayout layout = (LinearLayout) findViewById(R.id.favoritos_principal);
        layout.removeAllViews();  
                                     
		int k=0;
		int j=0;
		Log.v("AQUI", Main.favoritos.size()+"");
		if(Main.favoritos.size() <= Client.page_limit)
			j= Main.favoritos.size();
		else                                                //mudar estas contas - NAO TESTADO
			if(Main.favoritos.size() - pag_actual*Client.page_limit > 5)
				j=5;
			else j= Main.favoritos.size() - pag_actual*Client.page_limit;
		
		for(int i=(pag_actual-1)*Client.page_limit; k<j; ++i){
			k++;
			mov = Main.favoritos.get(i);
			
			// cria o layout principal (img + texto 1 + texto2)
			LinearLayout l1 = new LinearLayout(this);
			l1.setOrientation(LinearLayout.HORIZONTAL);

			// cria a imagem
			String nome = mov.getTitle();
			ImageView img = new ImageView(this);
			img.setId(mov.getId());
			img.setClickable(true);
		  	int mov_selected = img.getId();
		  	final int m2 = mov_selected;
			img.setOnClickListener(new View.OnClickListener() {
	             public void onClick(View v) {
	            	 Intent i = new Intent(List_favorites.this, Movieprofile.class);
	            	 i.putExtra("movie", m2); //passa o filme como argumento
	                 startActivity(i);	            	 
	             }
			});
			img.setLayoutParams(new GridView.LayoutParams(63, 90));  //tamanho da imagem
			draw.fetchDrawableOnThread(mov.getPoster_thumbnail(), img);
			
			// cria o layout secundario (texto 1 + checkbox)
			RelativeLayout l2 = new RelativeLayout(this);
			l2.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 
												LinearLayout.LayoutParams.FILL_PARENT));
			
			// texto 1 -titulo
			String titulo = mov.getTitle();			
			TextView title = new TextView(this);
			title.setText(titulo);
			title.setTextSize(18);
			title.setTextColor(Color.BLACK);
			title.setPadding(10,0,40,0);
			
			RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
			params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
			final CheckBox check = new CheckBox(this);
			check.setChecked(true);
			check.setLayoutParams(params);	
			check.setOnClickListener(new View.OnClickListener() {
	             public void onClick(View v) {
	            	 if(!check.isChecked()){
	            		 fav.removeFavorite(m2);
	            		 Intent i = new Intent(List_favorites.this, List_favorites.class);
	            		 startActivity(i);
	            	 }
	            	 if(check.isChecked()){
						Toast.makeText(getApplicationContext(), "Ja foi removido dos favoritos", Toast.LENGTH_LONG).show();
					}
	             }
			});

			l2.addView(title);
			l2.addView(check);
			l1.addView(img);
			l1.addView(l2);

			layout.addView(l1);			
		}
    }
}